import jaydebeapi
import sys
from gavial_scriptsdk.Constants import *


def get_jdbc_connection():
  """
  get jdbc connection by config
  uses jaydebeapi.connect and doc strings :-)
  https://pypi.python.org/pypi/JayDeBeApi/
  :return: jdbc connection
  """
  if STORAGE_TYPE == 'H2':
    driver_class = "org.h2.Driver"
    url = "jdbc:h2:tcp://localhost:%s/mem:%s" % (DB_PORT, DB)
    driver_args = [DB_USER, DB_PASSWORD]
    d = os.path.dirname(sys.modules['gavial_scriptsdk'].__file__)
    jars = os.path.join(d, 'h2-1.4.199.jar')
    conn = jaydebeapi.connect(driver_class, url, driver_args, jars)
    return conn
  else:
    return None
